export const usePermissionStore = defineStore('permission', () => {
  const routes = ref<RouteRecordRaw[]>([])
  const dynamicRoutes = ref<RouteRecordRaw[]>([])

  const generateRoutes = async (permissions: string[]) => {
    // 根据权限过滤路由
    const accessRoutes = filterAsyncRoutes(asyncRoutes, permissions)
    routes.value = constantRoutes.concat(accessRoutes)
    return accessRoutes
  }

  const hasPermission = (route: RouteRecordRaw, permissions: string[]) => {
    if (route.meta?.permission) {
      return permissions.includes(route.meta.permission as string)
    }
    return true
  }

  const filterAsyncRoutes = (routes: RouteRecordRaw[], permissions: string[]) => {
    const res: RouteRecordRaw[] = []
    routes.forEach(route => {
      if (hasPermission(route, permissions)) {
        if (route.children) {
          route.children = filterAsyncRoutes(route.children, permissions)
        }
        res.push(route)
      }
    })
    return res
  }

  return { routes, generateRoutes }
}) 